package com.zjj.algorithm.learning.leetcode.str;

/**
 * 67. 二进制求和 简单题
 * 给你两个二进制字符串 a 和 b ，以二进制字符串的形式返回它们的和。
 * <p>
 * <p>
 * <p>
 * 示例 1：
 * <p>
 * 输入:a = "11", b = "1"
 * 输出："100"
 * 示例 2：
 * <p>
 * 输入：a = "1010", b = "1011"
 * 输出："10101"
 *
 * @author zjj_admin
 * @date 2022/11/25 11:29
 */
public class AddBinary {

    public static void main(String[] args) {
        String a = "1111";
        String b = "1111";
        String res = addBinary2(a, b);
        System.out.println("res = " + res);

    }


    /**
     * 时间
     * 3 ms
     * 击败
     * 16.25%
     * 内存
     * 41.4 MB
     * 击败
     * 20.49%
     *
     * @param a
     * @param b
     * @return
     */
    public static String addBinary2(String a, String b) {
        int len1 = a.length();
        int len2 = b.length();
        int len = Math.max(len1, len2);
        for (int i = 0; i < len - len1; i++) {
            a = "0" + a;
        }
        for (int i = 0; i < len - len2; i++) {
            b = "0" + b;
        }
        int sup = 0;
        int l1;
        int l2;
        StringBuilder res = new StringBuilder();
        for (int i = len - 1; i >= 0; i--) {
            l1 = Integer.parseInt(a.substring(i, i + 1));
            l2 = Integer.parseInt(b.substring(i, i + 1));
            if (l1 + l2 + sup > 1) {
                if (l1 + l2 + sup == 2) {
                    res.insert(0, "0");
                } else {
                    res.insert(0, "1");
                }
                sup = 1;
            } else {
                int j = l1 + l2 + sup;
                res.insert(0, j);
                sup = 0;
            }
            if (i == 0) {
                if (l1 + l2 + sup > 1) {
                    res.insert(0, "1");
                }
            }
        }
        return res.toString();
    }

    /**
     * 时间
     * 2 ms
     * 击败
     * 29.37%
     * 内存
     * 41.7 MB
     * 击败
     * 9.20%
     *
     * @param a
     * @param b
     * @return
     */
    public static String addBinary1(String a, String b) {
        int len1 = a.length();
        int len2 = b.length();
        int len = Math.max(len1, len2);
        int[] arrA = new int[len];
        int[] arrB = new int[len];
        int[] arr = new int[len];
        int index1 = len1;
        int index2 = len2;
        //将数据传入到数组中
        for (int i = len; i > len - len1; i--) {
            arrA[i - 1] = Integer.parseInt(a.substring(index1 - 1, index1));
            index1--;
            if (index1 <= 0) {
                break;
            }
        }
        for (int i = len; i > len - len2; i--) {
            arrB[i - 1] = Integer.parseInt(b.substring(index2 - 1, index2));
            index2--;
            if (index2 <= 0) {
                break;
            }
        }
        int sup = 0;
        int l1;
        int l2;
        boolean isSup = false;
        for (int i = len - 1; i >= 0; i--) {
            l1 = arrA[i];
            l2 = arrB[i];
            if (l1 + l2 + sup > 1) {
                if (l1 + l2 + sup == 2) {
                    arr[i] = 0;
                } else {
                    arr[i] = 1;
                }
                sup = 1;
            } else {
                arr[i] = l1 + l2 + sup;
                sup = 0;
            }
            if (i == 0) {
                if (l1 + l2 + sup > 1) {
                    isSup = true;
                }
            }
        }
        StringBuilder res = new StringBuilder();
        for (int i = 0; i < arr.length; i++) {
            res.append(arr[i]);
        }
        if (isSup) {
            res.insert(0, "1");
        }
        return res.toString();
    }
}
